package com.example.onlinetaxi.BaiDuMap.Util;

import android.os.Handler;
import android.util.Log;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.example.onlinetaxi.BaiDuMap.BaiDuMapManager;
import com.example.onlinetaxi.callback.MapCallBack.IPickLocation;
import com.example.onlinetaxi.BaiDuMap.MyGeoCoder;

// 逆地理编码实现地图选点
public class GeoReverse implements BaiduMap.OnMapStatusChangeListener, OnGetGeoCoderResultListener {

    // 默认逆地理编码半径范围
    private static final int sDefaultRGCRadius = 500;
    // 地图View实例
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    // 地图中心点
    private LatLng mCenter;
    private Handler mHandler = MyGeoCoder.myGeoCoder.mHandler;
    // 地理反编码
    private GeoCoder mGeoCoder = MyGeoCoder.myGeoCoder.mGeoCoder;
    // 城市更换接口
    private IPickLocation iPickLocation;
    //
    private boolean mStatusChangeByItemClick = false;

    public void init(){
        // 初始化地图
        this.mMapView = BaiDuMapManager.baiDuMapManager.mMapView;
        this.mBaiduMap = BaiDuMapManager.baiDuMapManager.mBaiduMap;
        // 地图中心点（当前定位）
        this.mCenter = BaiDuMapManager.baiDuMapManager.getLocation();

        // 设置监听器
        mBaiduMap.setOnMapStatusChangeListener(this);
        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                reverseRequest(mCenter);
            }
        });
    }

    // 地图状态改变
    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        LatLng newCenter = mapStatus.target;

        // 如果是点击poi item导致的地图状态更新，则不用做后面的逆地理请求，
        if (mStatusChangeByItemClick) {
            if (!IsLatLngEquals.isLatlngEqual(mCenter, newCenter)) {
                mCenter = newCenter;
            }
            mStatusChangeByItemClick = false;
            return;
        }

        if (!IsLatLngEquals.isLatlngEqual(mCenter, newCenter)) {
            mCenter = newCenter;
            reverseRequest(mCenter);
        }
    }

    // 逆地理编码请求
    private void reverseRequest(LatLng latLng) {
        if (null == latLng) {
            return;
        }

        ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption().location(latLng)
                .newVersion(1) // 建议请求新版数据
                .radius(sDefaultRGCRadius);

        if (null == mGeoCoder) {
            mGeoCoder = GeoCoder.newInstance();
        }

        mGeoCoder.setOnGetGeoCodeResultListener(this);
        mGeoCoder.reverseGeoCode(reverseGeoCodeOption);
    }

    public void setiCityLocation(IPickLocation iPickLocation){
        this.iPickLocation = iPickLocation;
    }

    // 对结果进行处理
    @Override
    public void onGetReverseGeoCodeResult(final ReverseGeoCodeResult reverseGeoCodeResult) {
        if (null == reverseGeoCodeResult) {
            return;
        }

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // 对地图中心点进行选取
                iPickLocation.getPickInfo(reverseGeoCodeResult);
            }
        });
    }


    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {
    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus, int i) {
    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {

    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

    }

}
